﻿using Awsc.DataAcquisitionSys.Core.Common.Extensions;
using Awsc.DataAcquisitionSys.Core.DataAcquisition;
using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.ConfigManagement;
using Microsoft.Extensions.DependencyInjection;

namespace Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors.Sensors
{
    public class CylinderLiftSensor : Sensor
    {
        private readonly RegisterConfig config;
        private readonly DeviceTypeEnum deviceType;
        private readonly StringWeightSensorConfig abstractSensorConfig;
        public CylinderLiftSensor(AbstractSensorDataCollectionContext abstractSensorDataCollectionContext)
        {
            config = abstractSensorDataCollectionContext.SensorConfig as RegisterConfig;
            deviceType = abstractSensorDataCollectionContext.Service.GetService<IDeviceConfigService>().GetDeviceConfig().DeviceType;
            abstractSensorConfig = config.GetAdditionConfig<StringWeightSensorConfig>();
        }

        protected override void CalculateValue()
        {
            double d = 0;
            var status = DevStatus.S_OK;
            var liftingPressure = GlobalDeviceManager.Instance.GetSensorValue("举升压力");
            var liftingPressureSensor = GlobalDeviceManager.Instance.GetSensor("举升压力");
            var legMode = GlobalDeviceManager.Instance.GetSensorValue("腿模式");
            var legModeAICreated = GlobalDeviceManager.Instance.GetSensorValue("四腿模式Coil");
            if (liftingPressure != null)
            {
                double lv = liftingPressure.ToDouble() - abstractSensorConfig.NullLift;//举升压力修正值 
                double S = abstractSensorConfig.Lift_Cylinder_R / 2000 * abstractSensorConfig.Lift_Cylinder_R / 2000 * Math.PI;//单位平方米
                double s = S - abstractSensorConfig.Lift_Cylinder_Axle_R / 2000 * abstractSensorConfig.Lift_Cylinder_Axle_R / 2000 * Math.PI; //单位 平方米

                if (s > 0)
                {
                    var useLegMode = UseLegMode(legMode, legModeAICreated);
                    d = useLegMode ? S * lv * 1000000 * 4 / 9.8 * abstractSensorConfig.LiftRadio / 1000 : S * lv * 1000000 * 2 / 9.8 * abstractSensorConfig.LiftRadio / 1000;
                }
                this.CurrentValue = d;
                this.OutputValue = d;
                
            }

            else if (liftingPressureSensor.Status == DevStatus.S_UNKNOWN)
            {
                this.Status = DevStatus.S_UNKNOWN;

            }
            else
            {
                this.Status = DevStatus.S_ERR;

            }
        }

        private bool UseLegMode(SensorData legMode, SensorData legModeAI)
        {
            if (legModeAI == null && legMode == null)
            {
                return false;
            }
            if (legModeAI != null && legModeAI.ToBool())
            {
                return true;
            }
            if (legMode != null && legMode.ToBool())
            {
                return true;
            }
            return false;

        }
    }
}
